﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2ReportsSentiment
{
    public class ReportSentimentActions
    {
        public static int GetNumberFromAResult(string strSentimentCategory, string strSentimentCategoryItem, int intRowOfResult)
        {
            int intNumberFromTheFirst = 0;

            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv pnlWrapper = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlWrapper");
            HtmlDiv divReport = pnlWrapper.Find.ById<HtmlDiv>("divReport");
            HtmlSelect ddlLabel = divReport.Find.ById<HtmlSelect>("ddlLabel");
            SM2Input.Tradition.SelectItemSelectList(ddlLabel, strSentimentCategoryItem);
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);

            //Get number
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv UpdatePanel1 = pnlWrapper.Find.ById<HtmlDiv>("UpdatePanel1");
            HtmlDiv cphPageContent_SentimentGrid = UpdatePanel1.Find.ById<HtmlDiv>("cphPageContent_SentimentGrid");
            HtmlTable cphPageContent_grdSentiment = cphPageContent_SentimentGrid.Find.ById<HtmlTable>("cphPageContent_grdSentiment");
            
            while (!cphPageContent_grdSentiment.IsVisible())
            {
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                try
                {
                    pnlWrapper = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlWrapper");
                    UpdatePanel1 = pnlWrapper.Find.ById<HtmlDiv>("UpdatePanel1");
                    cphPageContent_SentimentGrid = UpdatePanel1.Find.ById<HtmlDiv>("cphPageContent_SentimentGrid");
                    cphPageContent_grdSentiment = cphPageContent_SentimentGrid.Find.ById<HtmlTable>("cphPageContent_grdSentiment");
                }
                catch { }
            }

            //Pick A row
            //Need to check if there is next page available or not
            int intRowNumberOfFirstResult = 0;
            if (hasNextPage()) 
                intRowNumberOfFirstResult = 1;
            else 
                intRowNumberOfFirstResult = 0;
            intRowNumberOfFirstResult += intRowOfResult;
            HtmlTableRow row = cphPageContent_grdSentiment.Rows[intRowNumberOfFirstResult];

            //Pick 3rd cell where Emotions be
            HtmlTableCell cell = row[2];

            //Look for table content all information
            IList<HtmlTable> iListTable = cell.Find.AllByAttributes<HtmlTable>();
            HtmlTable table = iListTable[0];

            //Loop over to pick number of expected Sentiment Category
            int intCountRows = table.Rows.Count; Boolean flag = false;
            for (int i = 0; i < intCountRows; i++)
            {
                HtmlTableRow rowSentimentItem = table.Rows[i];
                int intCountCells = rowSentimentItem.Cells.Count;
                for (int j = 0; j < intCountCells; j++)
                {
                    HtmlTableCell cell_SentimentCategory = rowSentimentItem[j];
                    IList<HtmlDiv> iListDiv = cell_SentimentCategory.Find.AllByAttributes<HtmlDiv>();
                    HtmlDiv div_name_SentimentCategory = iListDiv[0]; string str_div_name_SentimentCategory = div_name_SentimentCategory.TextContent.Trim();
                    HtmlDiv div_number_SentimentCategory = iListDiv[1]; string str_div_number_SentimentCategory = div_number_SentimentCategory.TextContent.Trim();
                    if (str_div_name_SentimentCategory.Equals(strSentimentCategory))
                    {
                        intNumberFromTheFirst = int.Parse(str_div_number_SentimentCategory);
                        flag = true;
                        break;
                    }
                }
                if (flag)
                    break;
            }

            Utilities.GlobalObject.LOGGER.log("Get number of results [" + strSentimentCategoryItem + " = " + intNumberFromTheFirst + "]");
            return intNumberFromTheFirst;
        }

        public static Boolean hasNextPage()
        {
            try
            {
                //Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                //HtmlDiv pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
                //HtmlDiv UpdatePanel1 = pnlContent.Find.ById<HtmlDiv>("UpdatePanel1");
                //HtmlTable cphPageContent_grdSentiment = UpdatePanel1.Find.ById<HtmlTable>("cphPageContent_grdSentiment");
                //HtmlAnchor cphPageContent_grdSentiment_btnPagerNext = cphPageContent_grdSentiment.Find.ById<HtmlAnchor>("cphPageContent_grdSentiment_btnPagerNext");

                Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
                HtmlDiv pnlContent = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("pnlContent");
                HtmlDiv UpdatePanel1 = pnlContent.Find.ById<HtmlDiv>("UpdatePanel1");
                HtmlTable cphPageContent_grdSentiment = UpdatePanel1.Find.ById<HtmlTable>("cphPageContent_grdSentiment");
                HtmlAnchor cphPageContent_grdSentiment_btnPagerNext = cphPageContent_grdSentiment.Find.ById<HtmlAnchor>("cphPageContent_grdSentiment_btnPagerNext");
                if (cphPageContent_grdSentiment_btnPagerNext.IsVisible())
                {
                    return true;
                }
                else return false;
            }
            catch
            {
                return false;
            }
        }
    }
}
